﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SQLite;
using Com.Toook.AutoDownloadHtml.Util;

public partial class jokedetail : System.Web.UI.Page
{
    protected string JokeTitle = "";
    protected string JokeContent = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            InitRandomList();
            InitJoke();
        }
    }
    private void InitJoke()
    {
        string id = Request["id"];
        int i;
        if(int.TryParse(id, out i))
        {
            string sql = "select title, content from UncheckedJokeHaha365 where id = " + id;
            SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);
            SQLiteDataReader reader = null;
            try
            {
                conn.Open();
                reader = cmd.ExecuteReader();
                if(reader.Read())
                {
                    JokeTitle = reader["title"].ToString();
                    JokeContent = reader["content"].ToString();
                }
            }
            finally
            {
                SqliteHelper.CloseDbResource(conn, reader);
            }
        }
    }
    private void InitRandomList()
    {
        string sql = "select id, title from UncheckedJokeHaha365 limit 15 ";
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        SQLiteDataReader reader = null;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            rptRandom.DataSource = reader;
            rptRandom.DataBind();
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }
    }
}
